import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login'
import Home from '@/views/home'
import Layout from '@/views/layout'
import Article from '@/views/article'
import Publish from '@/views/publish'
import Image from '@/views/image'
import Comment from '@/views/comment'
import Setting from '@/views/setting'
import Fans from '@/views/fans'
// 引入进度条显示
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 引入放大镜
import Maginify from '@/views/maginify'
// 一如loading
import Loading from '@/views/loading'
Vue.use(VueRouter)




// 设置路由重复点击报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 路由配置表
const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '/', // path 为空,会默认作为子路由渲染
        name: 'home',
        component: Home
      },
      {
        path: '/article',
        name: 'article',
        component: Article
      },
      {
        path: '/publish',
        name: 'publish',
        component: Publish
      },
      {
        path: '/image',
        name: 'image',
        component: Image
      },
      {
        path: '/comment',
        name: 'comment',
        component: Comment
      },
      {
        path: '/setting',
        name: 'setting',
        component: Setting
      },
      {
        path: '/fans',
        name: 'fans',
        component: Fans
      },
      {
        path: '/maginify',
        name: 'maginify',
        component: Maginify
      },
      {
        path: '/loading',
        name: 'loading',
        component: Loading
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫: 说白了所有页面的导航都会经过这里
// 守卫页面的导航的
// to: 要去的路由信息
// from: 来自哪里的路由信息
// next: 放行方法
router.beforeEach((to, from, next) => {
  // 如果要访问的页面不是 /login, 校验登录状态
  // 如果美誉登录,则跳转到登录页面
  // 如果登录了,则允许通过
  // 允许通过
  // next()
  const user = JSON.parse(window.localStorage.getItem('user'))

  if (to.path !== '/login') {
    if (user) {
      // 如果存在user说明已经登录,允许通过
      NProgress.start()
      next()
    } else {
      // 如果没有存在user说明没有登录,跳转到登录页面
      next('/login')
    }
  } else {
    // 登录页面,正常允许通过
    next()
  }
})
router.afterEach(() => {
  NProgress.done() // 完成进度条
})

NProgress.configure({     
  // showSpinner: false, // 关闭loading动画
  // trickle: false, // 关闭进度条步进
  // minimum: 0.3, // 最小百分比
  // ease: 'ease', // 过度动画
  // speed: 300, // 速度（毫秒）
  // trickleRate: 0.02, // 每次步进增长多少
  // trickleSpeed: 200 // 步进间隔
    easing: 'ease',  // 动画方式    
    speed: 500,  // 递增进度条的速度    
    showSpinner: false, // 是否显示加载ico    
    trickleSpeed: 200, // 自动递增间隔    
    minimum: 0.3 // 初始化时的最小百分比



})

export default router
